package com.lotusfall.mysport;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.lotusfall.mysport.controller.UserController;

import java.io.IOException;

public class MainActivity extends AppCompatActivity{
    Boolean user_isLogin;
    UserController userController = new UserController();
    TextView username;
    TextView password;
    TextView reg;
    Button login;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide(); // 隐藏标题栏

        SharedPreferences setting = getSharedPreferences("setting", 0);
        user_isLogin = setting.getBoolean("isLogin",true);
        //user_isLogin = true;
        if(user_isLogin){
            //未登录
            setContentView(R.layout.login);
            // 登陆按钮监听
            Button login = (Button)findViewById(R.id.btn_login);
            username = (TextView) findViewById(R.id.text_loginName);
            password = (TextView) findViewById(R.id.text_loginPassword);
            reg = (TextView)findViewById(R.id.btn_reg);
            reg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this, regActivity.class);
                    startActivity(intent);
                    finish();
                }
            });
            login.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    userController.login(getApplicationContext(),username.getText().toString(),password.getText().toString());
                    SystemClock.sleep(1000);
                    if(userController.flag==1){
                        Intent intent=new Intent(MainActivity.this, MyActivity.class);
                        startActivity(intent);
                        setting.edit().putBoolean("isLogin", false).commit();
                        finish();
                    }

                }
            });

        }else{
            //已登陆则直接跳转到下一个界面
            Intent intent=new Intent(this, MyActivity.class);
            startActivity(intent);
            finish();
        }



    }


}